package com.meida.module.bus.provider.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.export.styler.ExcelExportStylerDefaultImpl;
import cn.hutool.log.StaticLog;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.security.OpenHelper;
import com.meida.common.springmvc.base.BaseController;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.bus.client.entity.*;
import com.meida.module.bus.provider.api.util.PmsApiUtil;
import com.meida.module.bus.provider.service.*;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;


/**
 * 项目材料控制器
 *
 * @author flyme
 * @date 2020-10-09
 */
@RestController
@RequestMapping("/system/material/")
public class BusMaterialController extends BaseController<BusMaterialService, BusMaterial>  {
    @Autowired
    private BusMaterialPlan100Service busMaterialPlan100Service;
    @Autowired
    private BusMaterialPackage100Service busMaterialPackage100Service;
    @Autowired
    private BusProjectService busProjectService;
    @Autowired
    private BusVersionService busVersionService;

    @Autowired
    private PmsApiUtil pmsApiUtil;

    @Autowired
    private BusMaterialViewService busMaterialViewService;

    @ApiOperation(value = "项目材料-分页列表", notes = "项目材料分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        ResultBody rsb = bizService.pageMaterList(params);
        PmsApiUtil.levelProcess(rsb);
        return rsb;
    }

    @ApiOperation(value = "项目材料-列表", notes = "项目材料列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "项目材料-添加", notes = "添加项目材料")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
       BusProject project=busProjectService.getById(Long.parseLong(params.get("projectId").toString()));
       if(project.getVersion100()==1){
           return ResultBody.failed("该项目已转存100不支持添加");
       }
        return bizService.add(params);
    }

    @ApiOperation(value = "项目材料-更新", notes = "更新项目材料")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        BusProject project=busProjectService.getById(Long.parseLong(params.get("projectId").toString()));
        if(project.getVersion100()==1){
            return ResultBody.failed("该项目已转存100不支持添加");
        }
        return bizService.updateMateialdetail(params);
    }

    @ApiOperation(value = "项目材料-删除", notes = "删除项目材料")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        BusProject project=busProjectService.getById(Long.parseLong(params.get("projectId").toString()));
        if(project.getVersion100()==1){
            return ResultBody.failed("该项目已转存100不支持添加");
        }
        return bizService.deleteMaterial(Long.parseLong(params.get("ids").toString()));
    }
    @ApiOperation(value = "项目材料-全删除", notes = "项目材料-全删除")
    @PostMapping(value = "removeall")
    public ResultBody removeall(@RequestParam(required = false) Map params) {
        BusProject project=busProjectService.getById(Long.parseLong(params.get("projectId").toString()));
        if(project.getVersion100()==1){
            return ResultBody.failed("该项目已转存100不能删除");
        }
        return bizService.deleteallMaterial(project.getProjectId());
    }
    @ApiOperation(value = "项目材料-详情", notes = "项目材料详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        EntityMap entityMap= bizService.getBusMaterialView(params);
        return ResultBody.ok(entityMap);
    }
    /**
     * 导出项目材料统计
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        TemplateExportParams templateExportParams=new TemplateExportParams("excel/母表清单导出模板.xls",true);
        templateExportParams.setStyle(ExcelExportStylerDefaultImpl.class);
         Map<String, Object> reportBodyMap =  new HashMap<>();
         List<Map<String, Object>> listMap = new ArrayList<>();
         List<EntityMap> busMaterialList = bizService.exceportData(params);
        for (EntityMap entityMap: busMaterialList) {
            Map<String,Object> objectMap=new HashMap<>();
            objectMap.put("c1",entityMap.get("majorName"));  objectMap.put("c2",entityMap.get("majorCode"));
            objectMap.put("c3",entityMap.get("packageName"));  objectMap.put("c4",entityMap.get("packageNo"));
            objectMap.put("c5",entityMap.get("materialWbsCode")); objectMap.put("c6",entityMap.get("materialCode"));
            objectMap.put("c7",entityMap.get("materialName")); objectMap.put("c8",entityMap.get("materiaSpecs"));
            objectMap.put("c9",entityMap.get("materialDesc"));objectMap.put("c10",entityMap.get("technicalData"));
            objectMap.put("c11",entityMap.get("serviceWbsCode"));objectMap.put("c12",entityMap.get("serviceCode"));
            objectMap.put("c13",entityMap.get("subjectName"));objectMap.put("c14",entityMap.get("packageDesc"));
            objectMap.put("c15",entityMap.get("levelNo"));objectMap.put("c16",entityMap.get("contractDesc"));
            objectMap.put("c17",entityMap.get("materialArea"));objectMap.put("c18",entityMap.get("materialUse"));
            objectMap.put("c19",entityMap.getBigDecimal("contractBillQuantity"));objectMap.put("c20",entityMap.getBigDecimal("beforeMarkQuantity"));
            objectMap.put("c21",entityMap.getBigDecimal("costQuantity100"));objectMap.put("c22",entityMap.getBigDecimal("costQuantity200"));
            objectMap.put("c23",entityMap.getBigDecimal("loss").toPlainString());objectMap.put("c24",entityMap.get("measurementUnit"));
            objectMap.put("c25",entityMap.getBigDecimal("planPurchaseCount").toPlainString());objectMap.put("c26",entityMap.getBigDecimal("planPurchaseCount").toPlainString());
            objectMap.put("c27",entityMap.getBigDecimal("currentFinishMonthCount").toPlainString());objectMap.put("c28",entityMap.getBigDecimal("lastFinishMonthCount").toPlainString());
            objectMap.put("c29",entityMap.getBigDecimal("totalFinishCount").toPlainString());objectMap.put("c30",entityMap.getBigDecimal("mainPrice").toPlainString());
            objectMap.put("c31",entityMap.getBigDecimal("installPrice").toPlainString());objectMap.put("c32",entityMap.getBigDecimal("syntheticalPrice").toPlainString());
            objectMap.put("c33",entityMap.getBigDecimal("mainAmount").toPlainString());objectMap.put("c34",entityMap.getBigDecimal("installAmount").toPlainString());
            objectMap.put("c35",entityMap.getBigDecimal("syntheticalAmount").toPlainString());objectMap.put("c36",entityMap.getBigDecimal("mainPrice1").toPlainString());
            objectMap.put("c37",entityMap.getBigDecimal("installPrice1").toPlainString());objectMap.put("c38",entityMap.getBigDecimal("syntheticalPrice1").toPlainString());
            objectMap.put("c39",entityMap.getBigDecimal("mainPrice2").toPlainString());objectMap.put("c40",entityMap.getBigDecimal("installPrice2").toPlainString());
            objectMap.put("c41",entityMap.getBigDecimal("syntheticalPrice2").toPlainString());objectMap.put("c42",entityMap.getBigDecimal("mainPrice3").toPlainString());
            objectMap.put("c43",entityMap.getBigDecimal("installPrice3").toPlainString());objectMap.put("c44",entityMap.getBigDecimal("syntheticalPrice3").toPlainString());
            objectMap.put("c45",entityMap.getBigDecimal("mainsumPrice1").toPlainString());objectMap.put("c46",entityMap.getBigDecimal("installsumPrice1").toPlainString());
            objectMap.put("c47",entityMap.getBigDecimal("contractsumSyntheticalPrice1").toPlainString());objectMap.put("c48",entityMap.getBigDecimal("mainsumPrice2").toPlainString());
            objectMap.put("c49",entityMap.getBigDecimal("installsumPrice2").toPlainString());objectMap.put("c50",entityMap.getBigDecimal("contractsumSyntheticalPrice2").toPlainString());
            objectMap.put("c51",entityMap.getBigDecimal("mainsumPrice3").toPlainString());objectMap.put("c52",entityMap.getBigDecimal("installsumPrice3").toPlainString());
            objectMap.put("c53",entityMap.getBigDecimal("contractsumSyntheticalPrice3").toPlainString());objectMap.put("c54",entityMap.getBigDecimal("currentMonthmainPrice").toPlainString());
            objectMap.put("c55",entityMap.getBigDecimal("currentMonthinstallPrice").toPlainString());objectMap.put("c56",entityMap.getBigDecimal("currentMonthsumPrice").toPlainString());
            objectMap.put("c57",entityMap.getBigDecimal("lastMonthmainPrice").toPlainString());objectMap.put("c58",entityMap.getBigDecimal("lastMonthinstallPrice").toPlainString());
            objectMap.put("c59",entityMap.getBigDecimal("lastMonthsumPrice").toPlainString());objectMap.put("c60",entityMap.getBigDecimal("totalMonthmainPrice").toPlainString());
            objectMap.put("c61",entityMap.getBigDecimal("totalMonthinstallPrice").toPlainString());objectMap.put("c62",entityMap.getBigDecimal("totalsumPrice").toPlainString());
            objectMap.put("c63",entityMap.get("brandName"));objectMap.put("c64",entityMap.get("remark"));
            listMap.add(objectMap);
        }
        reportBodyMap.put("maplist", listMap);
        Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, reportBodyMap);
        File savefile = new File("D:/excel/");
        if (!savefile.exists()) {
            savefile.mkdirs();
        }
        String fileName="工程清单列表.xls";
        try {
            response.reset();
            response.setContentType("application/octet-stream;charset=UTF-8");
            fileName = URLEncoder.encode(fileName, "UTF-8");
            response.setHeader("Content-Disposition", "attachment;filename="
                + fileName);
            OutputStream fos = response.getOutputStream();
            workbook.write(fos);
            fos.close();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    /**
     * 转存100版本
     *
     *
     */
    @PostMapping(value = "zcversion")
    public ResultBody zcversion(@RequestParam Long[] materialPlanIds,Long projectId) {
        return bizService.updataStatus(materialPlanIds,projectId);
    }


    /**
     * 导出项目材料统计
     *
     * @param params
     * @param response
     */
    @GetMapping(value = "download")
    public void download(@RequestParam(required = false) Map params, HttpServletResponse response, Workbook workbook) {
        bizService.getexcel("PMS材料分包各版本可编辑列表",response, workbook);
    }


    /**
     * 导入项目材料母表数据
     *
     * @param params
     */
    @PostMapping(value = "importExcel")
    public ResultBody importExcel(@RequestParam(required = false) Map params, @RequestParam("file") MultipartFile file,Long projectId) throws Exception {
        BusProject project = busProjectService.getById(projectId);
        if (FlymeUtils.isNotEmpty(project)) {
            List<String> arrays = new ArrayList<>();
            List<BusMaterial> busMaterials = new ArrayList<>();
            List<BusMaterialPackage100> busMaterialPackage100List = new ArrayList<>();
            List<BusMaterialPlan100> materialPlan100s = new ArrayList<>();
            List<Map<Object, Object>> ExcelList = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, new ImportParams());
            Long veersionId = IdWorker.getId();
            BusVersion version = new BusVersion();
            version.setVersionId(veersionId);
            version.setUserId(OpenHelper.getUserId());
            version.setProjectId(projectId);
            version.setBigVersion("100");
            version.setSmallVerion("00");
            version.setProjectId(projectId);
            version.setState(1);//1正常
            version.setVersionType(1);

            Long packageVesionId = IdWorker.getId();
            BusVersion pversion = new BusVersion();
            pversion.setVersionId(packageVesionId);
            pversion.setUserId(OpenHelper.getUserId());
            pversion.setProjectId(projectId);
            pversion.setBigVersion("100");
            pversion.setSmallVerion("00");
            pversion.setProjectId(projectId);
            pversion.setState(1);//1正常
            pversion.setVersionType(2);
            int i = 1;
            List<String> levelNos = Lists.newArrayList();
            for (Map<Object, Object> objectObjectMap : ExcelList) {
                objectObjectMap.forEach((k,v)->{
                    if(v==null||v.equals("null")){
                        v="";
                        objectObjectMap.put(k,v);
                    }
                });
                i = ExcelList.indexOf(objectObjectMap) + 2;
                if (ObjectUtils.isEmpty(objectObjectMap.get("层级号"))) {
                    return ResultBody.failed("请查看第" + i + "行层级号为空");
                }else{
                    String leavlNo=String.valueOf(objectObjectMap.get("层级号"));
                    if(leavlNo.length()!=11){
                        return ResultBody.failed("请查看第" + i + "行层级号位数不正确");
                    }
                    if(levelNos.contains(leavlNo)) {
                        return ResultBody.failed("请查看第" + i + "行层级号【"+leavlNo+"】重复");
                    } else {
                        levelNos.add(leavlNo);
                    }
                }
                if (!objectObjectMap.get("层级号").toString().substring(0, 5).equals(objectObjectMap.get("工程包代码").toString())) {
                    return ResultBody.failed("请查看第" + i + "行 层级号【"+objectObjectMap.get("层级号")+"】为与工程包代码【"+objectObjectMap.get("工程包代码")+"】不一致");
                }
                if (ObjectUtils.isEmpty(objectObjectMap.get("服务代码")) && ObjectUtils.isEmpty(objectObjectMap.get("材料WBS代码"))) {
                    return ResultBody.failed("请查看第" + i + "行服务代码与材料wbs代码同时为空");
                }
                // TODO: 计量单位暂时不校验
//                if(StrUtil.isNotEmpty(String.valueOf(objectObjectMap.get("计量单位"))) &&
//                    StrUtil.isEmpty(pmsApiUtil.getUnitCode(String.valueOf(objectObjectMap.get("计量单位"))))) {
//                    return ResultBody.failed("请查看第" + i + "行的计量单位未在系统中维护");
//                }
                String majorCode = String.valueOf(objectObjectMap.get("专业包代码"));
                String majorName = String.valueOf(objectObjectMap.get("专业包名称"));
                String projectCode = String.valueOf(objectObjectMap.get("工程包代码"));
                String projectName = String.valueOf(objectObjectMap.get("工程包名称"));
                String materialCode = String.valueOf(objectObjectMap.get("全物料代码"));
                String materialWbsCode = String.valueOf(objectObjectMap.get("材料WBS代码"));
                String serviceCode = String.valueOf(objectObjectMap.get("服务代码"));
                String serviceWbsCode = String.valueOf(objectObjectMap.get("服务WBS代码"));
                String materialName = String.valueOf(objectObjectMap.get("设备、材料名称"));
                // String materiaSpecs = String.valueOf(objectObjectMap.get("型号/规格"));
                String materiaSpecs = String.valueOf(objectObjectMap.get("项目名称"));
                String technicalData = String.valueOf(objectObjectMap.get("技术参数或技术说明"));
                String subjectName = String.valueOf(objectObjectMap.get("科目名称"));
                String levelNo = String.valueOf(objectObjectMap.get("层级号"));
                String materialDesc = String.valueOf(objectObjectMap.get("材料项目描述"));
                String contractDesc = String.valueOf(objectObjectMap.get("对外合同清单描述"));
                String materialArea = String.valueOf(objectObjectMap.get("分区域"));
                String materialUse = String.valueOf(objectObjectMap.get("分系统"));
                BigDecimal contractBillQuantity = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("合同清单工程量"))) {
                    String str=objectObjectMap.get("合同清单工程量").toString();
                    System.out.println(str);
                    contractBillQuantity = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("合同清单工程量").toString()));
                }
                BigDecimal beforeMarkQuantity = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("标前工程量"))) {
                    beforeMarkQuantity = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("标前工程量").toString()));
                }
                BigDecimal costQuantity100 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("100预算成本工程量"))) {
                    costQuantity100 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("100预算成本工程量").toString()));

                }
                BigDecimal costQuantity200 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("200预算成本工程量"))) {
                    costQuantity200 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("200预算成本工程量").toString()));
                }
                /**
                 * 成工程量_本月完成工程量、完成工程量_截止上月完成工程量、完成工程量_累计完成工程量= 导入时无数据
                 */
                String measurementUnit = String.valueOf(objectObjectMap.get("计量单位"));
                String conversionUnit = String.valueOf(objectObjectMap.get("换算单位"));
                BigDecimal loss = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("损耗"))) {
                    loss = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("损耗").toString()));
                }
                String packageDesc = String.valueOf(objectObjectMap.get("分承包发包项目描述"));



                /*****************************预算成本所有计算***************************/
                //=======预算成本单价_未税_单价_主材价 A
                //=======预算成本单价_未税_单价_安装价 B
                //======预算成本单价_未税_单价_综合单价 A+B
                //===主材费=主材价*100工程量+主材价*200工程量
                BigDecimal mainPrice = BigDecimal.ZERO;
                BigDecimal mainAmount = BigDecimal.ZERO;
                BigDecimal installAmount = BigDecimal.ZERO;
                BigDecimal syntheticalPrice = BigDecimal.ZERO;
                BigDecimal syntheticalAmount = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("预算成本单价_未税_单价_主材价"))) {
                    mainPrice = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("预算成本单价_未税_单价_主材价").toString()));
                }
                BigDecimal installPrice = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("预算成本单价_未税_单价_安装价"))) {
                    installPrice = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("预算成本单价_未税_单价_安装价").toString()));
                }
                mainAmount = mainAmount.add(costQuantity100.multiply(mainPrice)).add(costQuantity200.multiply(mainPrice));//主材费
                installAmount = installAmount.add(costQuantity100.multiply(installPrice)).add(costQuantity200.multiply(installPrice));//安装费
                syntheticalAmount = syntheticalAmount.add(mainAmount).add(installAmount);//预算成本 综合费合价
                syntheticalPrice = syntheticalPrice.add(mainPrice).add(installPrice);//成本综合单价
                /*********************************有合同相关计算****************************************/

                BigDecimal contractMainPrice1 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("对外合同单价_有合同价_单价_主材价"))) {
                    contractMainPrice1 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("对外合同单价_有合同价_单价_主材价").toString()));
                }
                BigDecimal contractInstallPrice1 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("对外合同单价_有合同价_单价_安装价"))) {
                    contractInstallPrice1 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("对外合同单价_有合同价_单价_安装价").toString()));
                }
                BigDecimal syntheticalPrice1 = BigDecimal.ZERO;
                BigDecimal mainAmount1 = BigDecimal.ZERO;
                BigDecimal installAmount1 = BigDecimal.ZERO;
                BigDecimal syntheticalAmount1 = BigDecimal.ZERO;
                syntheticalPrice1 = syntheticalPrice1.add(contractMainPrice1.add(contractInstallPrice1));//有合同综合单价
                mainAmount1 = mainAmount1.add(costQuantity100.multiply(contractMainPrice1)).add(costQuantity200.multiply(contractMainPrice1));//主材费
                installAmount1 = installAmount1.add(costQuantity100.multiply(contractInstallPrice1)).add(costQuantity200.multiply(contractInstallPrice1));//安装费
                syntheticalAmount1 = syntheticalAmount1.add(mainAmount1.add(installAmount1));
                /*********************************拟合同相关计算****************************************/

                BigDecimal contractMainPrice2 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("对外合同单价_拟合同价单价_主材价"))) {
                    contractMainPrice2 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("对外合同单价_拟合同价单价_主材价").toString()));
                }
                BigDecimal contractInstallPrice2 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("对外合同单价_拟合同价单价_安装价"))) {
                    contractInstallPrice2 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("对外合同单价_拟合同价单价_安装价").toString()));
                }
                //拟合同综合价计算
                BigDecimal syntheticalPrice2 = BigDecimal.ZERO;
                BigDecimal mainAmount2 = BigDecimal.ZERO;
                BigDecimal installAmount2 = BigDecimal.ZERO;
                BigDecimal syntheticalAmount2 = BigDecimal.ZERO;
                syntheticalPrice2 = syntheticalPrice2.add(contractMainPrice2.add(contractInstallPrice2));//拟合同综合单价
                mainAmount2 = mainAmount2.add(costQuantity100.multiply(contractMainPrice2)).add(costQuantity200.multiply(contractMainPrice2));//主材费
                installAmount2 = installAmount2.add(costQuantity100.multiply(contractInstallPrice2)).add(costQuantity200.multiply(contractInstallPrice2));//安装费
                syntheticalAmount2 = syntheticalAmount2.add(mainAmount2.add(installAmount2));

                /*********************************合同相关计算****************************************/

                BigDecimal contractMainPrice3 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("对外合同单价_合同价_单价_主材价"))) {
                    contractMainPrice3 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("对外合同单价_合同价_单价_主材价").toString()));
                }
                BigDecimal contractInstallPrice3 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("对外合同单价_合同价_单价_安装价"))) {
                    contractInstallPrice3 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("对外合同单价_合同价_单价_安装价").toString()));
                }
                BigDecimal syntheticalPrice3 = BigDecimal.ZERO;
                BigDecimal mainAmount3 = BigDecimal.ZERO;
                BigDecimal installAmount3 = BigDecimal.ZERO;
                BigDecimal syntheticalAmount3 = BigDecimal.ZERO;
                contractMainPrice3=contractMainPrice1.add(contractMainPrice2);//主材价=拟合同+有合同===主材
                contractInstallPrice3=contractInstallPrice1.add(contractInstallPrice2);//安装
                syntheticalPrice3 = syntheticalPrice3.add(contractMainPrice3.add(contractInstallPrice3));
                mainAmount3 = mainAmount3.add(costQuantity100.multiply(contractMainPrice3)).add(costQuantity200.multiply(contractMainPrice3));//主材费
                installAmount3 = installAmount3.add(costQuantity100.multiply(contractInstallPrice3)).add(costQuantity200.multiply(contractInstallPrice3));//安装费
                syntheticalAmount3 = syntheticalAmount3.add(mainAmount3.add(installAmount3));
                /*********************************预估有合同合计****************************************/
                BigDecimal installsumPrice1 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("预估总产值_有合同价_合价_安装价"))) {
                    installsumPrice1 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("预估总产值_有合同价_合价_安装价").toString()));
                }
                BigDecimal mainsumPrice1 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("预估总产值_有合同价_合价_主材价"))) {
                    mainsumPrice1 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("预估总产值_有合同价_合价_主材价").toString()));
                }
                mainsumPrice1=mainsumPrice1.add(contractMainPrice1);
                mainsumPrice1=mainsumPrice1.multiply(costQuantity100);

                installsumPrice1=installsumPrice1.add(contractInstallPrice1);
                installsumPrice1=installsumPrice1.multiply(costQuantity100);
                BigDecimal contractsumSyntheticalPrice1 = BigDecimal.ZERO;
                contractsumSyntheticalPrice1 = contractsumSyntheticalPrice1.add(mainsumPrice1.add(installsumPrice1));

                /*********************************预估拟合同合计****************************************/
                BigDecimal installsumPrice2 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("预估总产值_拟合同价_合价_安装价"))) {
                    installsumPrice2 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("预估总产值_拟合同价_合价_安装价").toString()));
                }
                BigDecimal mainsumPrice2 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("预估总产值_拟合同价_合价_主材价"))) {
                    mainsumPrice2 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("预估总产值_拟合同价_合价_主材价").toString()));
                }
                mainsumPrice2=mainsumPrice2.add(contractMainPrice2);
                mainsumPrice2=mainsumPrice2.multiply(costQuantity100);
                installsumPrice2=installsumPrice2.add(contractInstallPrice2);
                installsumPrice2=installsumPrice2.multiply(costQuantity100);
                BigDecimal contractsumSyntheticalPrice2 = BigDecimal.ZERO;
                contractsumSyntheticalPrice2 = contractsumSyntheticalPrice2.add(mainsumPrice2).add(installsumPrice2);

                /*********************************预估合同合计****************************************/
                BigDecimal installsumPrice3 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("预估总产值_合同价_合价_安装价"))) {
                    installsumPrice3 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("预估总产值_合同价_合价_安装价").toString()));
                }
                BigDecimal mainsumPrice3 = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("预估总产值_合同价_合价_主材价"))) {
                    mainsumPrice3 = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("预估总产值_合同价_合价_主材价").toString()));
                }
                mainsumPrice3=mainsumPrice3.add(contractMainPrice3);
                mainsumPrice3=mainsumPrice3.multiply(costQuantity100);
                installsumPrice3=installsumPrice3.add(contractInstallPrice3);
                installsumPrice3=installsumPrice3.multiply(costQuantity100);
                BigDecimal contractsumSyntheticalPrice3 = BigDecimal.ZERO;
                contractsumSyntheticalPrice3 = contractsumSyntheticalPrice3.add(mainsumPrice3.add(installsumPrice3));
                BigDecimal planPurchaseCount = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("采购计划计量单位合计"))) {
                    planPurchaseCount = costQuantity100.multiply(loss);
                }

                BigDecimal convertPurchaseCount = BigDecimal.ZERO;
                if (FlymeUtils.isNotEmpty(objectObjectMap.get("采购计划换算单位合计"))) {
                    convertPurchaseCount = costQuantity100.multiply(loss);

                }
                /***============================完成产值初始化计算====================================**/
//                BigDecimal convertPurchaseCount = BigDecimal.ZERO;
//                if (FlymeUtils.isNotEmpty(objectObjectMap.get("采购计划换算单位合计"))) {
//                    convertPurchaseCount = BigDecimal.valueOf(Double.parseDouble(objectObjectMap.get("采购计划换算单位合计").toString())).multiply(loss);
//                    ;
//                }
                String brandName = String.valueOf(objectObjectMap.get("品牌"));
                String remark = String.valueOf(objectObjectMap.get("备注"));
                if (ObjectUtils.isNotEmpty(objectObjectMap.get("服务代码")) && ObjectUtils.isNotEmpty(objectObjectMap.get("全物料代码"))) {
                    BusMaterialPackage100 busPackage100 = new BusMaterialPackage100();
                    Long id = IdWorker.getId();
                    busPackage100.setMaterialPackage100Id(id);
                    BusMaterial busMaterial = new BusMaterial();
                    busMaterial.setParentId(0L);
                    if (FlymeUtils.isNotEmpty(levelNo)) {
                        if (levelNo.length() == 5) {
                            arrays.add(levelNo);
                        } else {
                            if (FlymeUtils.isNotEmpty(arrays)) {
                                for (String d : arrays) {
                                    if (d.equals(levelNo.substring(0, 5))) {
                                        Map<String, Object> busplanMap = Maps.newHashMap();
                                        busplanMap.put("levelNo", d);
                                        List<BusMaterialPlan100> plan100List = (List<BusMaterialPlan100>) busMaterialPlan100Service.listByMap(busplanMap);
                                        if (FlymeUtils.isNotEmpty(plan100List)) {
                                            busMaterial.setParentId(plan100List.get(0).getMaterialId());
                                        }
                                    }
                                }
                            }
                        }
                    }

                    busMaterial.setBeforeMarkQuantity(beforeMarkQuantity);
                    busMaterial.setMaterialId(id);
                    busMaterial.setProjectId(projectId);
                    busMaterial.setConversionUnit(conversionUnit);
                    busMaterial.setMeasurementUnit(measurementUnit);
                    busMaterial.setMainPrice(BigDecimal.ZERO);
                    busMaterial.setMainPrice1(BigDecimal.ZERO);
                    busMaterial.setMainPrice2(BigDecimal.ZERO);
                    busMaterial.setMainPrice3(BigDecimal.ZERO);
                    busMaterial.setInstallPrice(installPrice);
                    busMaterial.setInstallPrice1(contractInstallPrice1);
                    busMaterial.setInstallPrice2(contractInstallPrice2);
                    busMaterial.setInstallPrice3(contractInstallPrice3);
                    busMaterial.setSyntheticalPrice1(contractInstallPrice1);//有合同综合价
                    busMaterial.setSyntheticalPrice2(contractInstallPrice2);//拟合同综合价
                    busMaterial.setSyntheticalPrice3(contractInstallPrice3);//合同综合价
                    busMaterial.setSyntheticalPrice(installPrice);//预算成本综合价
                    busMaterial.setSyntheticalAmount(installPrice.multiply(costQuantity100));//预算成本合计
                    busMaterial.setSyntheticalAmount1(contractInstallPrice1.multiply(costQuantity100));//有合同预算成本合计
                    busMaterial.setSyntheticalAmount2(contractInstallPrice2.multiply(costQuantity100));//拟合同预算成本合计
                    busMaterial.setSyntheticalAmount3(contractInstallPrice3.multiply(costQuantity100));//合同预算成本合计
                    busMaterial.setContractBillQuantity(contractBillQuantity);
                    busMaterial.setMainAmount(BigDecimal.ZERO);//成本主材费
                    busMaterial.setMainAmount1(BigDecimal.ZERO);
                    busMaterial.setMainAmount2(BigDecimal.ZERO);
                    busMaterial.setMainAmount3(BigDecimal.ZERO);
                    busMaterial.setInstallAmount(installAmount);//成本安装价
                    busMaterial.setInstallAmount1(installAmount1);
                    busMaterial.setInstallAmount2(installAmount2);
                    busMaterial.setInstallAmount3(installAmount3);
                    busMaterial.setMainsumPrice1(BigDecimal.ZERO);
                    busMaterial.setMainsumPrice2(BigDecimal.ZERO);
                    busMaterial.setMainsumPrice3(BigDecimal.ZERO);
                    busMaterial.setInstallsumPrice1(installsumPrice1);
                    busMaterial.setInstallsumPrice2(installsumPrice2);
                    busMaterial.setInstallsumPrice3(installsumPrice3);
                    busMaterial.setContractsumSyntheticalPrice1(installsumPrice1);
                    busMaterial.setContractsumSyntheticalPrice2(installsumPrice2);
                    busMaterial.setContractsumSyntheticalPrice3(installsumPrice3);
                    busMaterial.setLoss(loss);
                    busMaterial.setContractBillQuantity(contractBillQuantity);
                    busMaterial.setPlanPurchaseCount(costQuantity100.multiply(loss));
                    busMaterial.setConvertPurchaseCount(costQuantity100.multiply(loss));
                    busMaterial.setParentId(0L);
                    busMaterial.setCurrentMonthmainPrice(BigDecimal.ZERO);
                    busMaterial.setCurrentMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial.setCurrentMonthsumPrice(BigDecimal.ZERO);
                    busMaterial.setLastMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial.setLastMonthmainPrice(BigDecimal.ZERO);
                    busMaterial.setLastMonthsumPrice(BigDecimal.ZERO);
                    busMaterial.setTotalsumPrice(BigDecimal.ZERO);
                    busMaterial.setTotalMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial.setTotalMonthmainPrice(BigDecimal.ZERO);
                    busMaterials.add(busMaterial);
                    busPackage100.setProjectId(projectId);
                    busPackage100.setMaterialPackage100Id(id);
                    busPackage100.setTopParentId(id);
                    busPackage100.setParentId(0L);
                    busPackage100.setVersionId(packageVesionId);
                    busPackage100.setMaterialId(id);
                    busPackage100.setLevelNo(levelNo);
                    busPackage100.setMajorName(majorName);
                    busPackage100.setMajorCode(majorCode);
                    busPackage100.setPackageName(projectName);
                    busPackage100.setPackageNo(projectCode);
                    busPackage100.setServiceCode(serviceCode);
                    busPackage100.setServiceWbsCode(serviceWbsCode);
                    busPackage100.setSubjectName(subjectName);
                    busPackage100.setPackageDesc(packageDesc);
                    busPackage100.setContractDesc(contractDesc);
                    busPackage100.setMaterialArea(materialArea);
                    busPackage100.setMaterialUse(materialUse);
                    busPackage100.setCostQuantity100(costQuantity100);
                    busPackage100.setCostQuantity200(costQuantity200);
                    busPackage100.setPackageDesc(packageDesc);
                    busPackage100.setContractInstallPrice1(contractInstallPrice1);
                    busPackage100.setContractInstallPrice2(contractInstallPrice2);
                    busPackage100.setContractInstallPrice3(contractInstallPrice3);
                    busPackage100.setContractOtherPrice1(syntheticalPrice1);
                    busPackage100.setContractOtherPrice2(syntheticalPrice2);
                    busPackage100.setContractOtherPrice3(syntheticalPrice3);
                    busPackage100.setStatus(0);//初始化导入状态 0 未转100 1转为1
                    busPackage100.setVersionStatus(1);
                    busPackage100.setApplayStatus(null);//未审批状态
                    busPackage100.setMateriaSpecs(materiaSpecs);//项目名称
                    busMaterialPackage100List.add(busPackage100);

                    BusMaterialPlan100 busPlan100 = new BusMaterialPlan100();
                    BusMaterial busMaterial2 = new BusMaterial();
                    busMaterial2.setBeforeMarkQuantity(beforeMarkQuantity);
                    busMaterial2.setMaterialId(id);
                    busMaterial2.setProjectId(projectId);
                    busMaterial2.setConversionUnit(conversionUnit);
                    busMaterial2.setMeasurementUnit(measurementUnit);
                    busMaterial2.setMainPrice(mainPrice);
                    busMaterial2.setMainPrice1(contractMainPrice1);
                    busMaterial2.setMainPrice2(contractMainPrice2);
                    busMaterial2.setMainPrice3(contractMainPrice3);
                    busMaterial2.setInstallPrice(BigDecimal.ZERO);
                    busMaterial2.setInstallPrice1(BigDecimal.ZERO);
                    busMaterial2.setInstallPrice2(BigDecimal.ZERO);
                    busMaterial2.setInstallPrice3(BigDecimal.ZERO);
                    busMaterial2.setSyntheticalPrice1(contractMainPrice1);//有合同综合价
                    busMaterial2.setSyntheticalPrice2(contractMainPrice2);//拟合同综合价
                    busMaterial2.setSyntheticalPrice3(contractMainPrice3);//合同综合价
                    busMaterial2.setSyntheticalPrice(mainPrice);//预算成本综合价
                    busMaterial2.setSyntheticalAmount(mainPrice.multiply(costQuantity100));//预算成本合计
                    busMaterial2.setSyntheticalAmount1(contractMainPrice1.multiply(costQuantity100));//有合同预算成本合计
                    busMaterial2.setSyntheticalAmount2(contractMainPrice2.multiply(costQuantity100));//拟合同预算成本合计
                    busMaterial2.setSyntheticalAmount3(contractMainPrice3.multiply(costQuantity100));//合同预算成本合计
                    busMaterial2.setContractBillQuantity(contractBillQuantity);
                    busMaterial2.setMainAmount(mainAmount);//成本主材费
                    busMaterial2.setMainAmount1(mainAmount1);
                    busMaterial2.setMainAmount2(mainAmount2);
                    busMaterial2.setMainAmount3(mainAmount3);
                    busMaterial2.setInstallAmount(BigDecimal.ZERO);//成本安装价
                    busMaterial2.setInstallAmount1(BigDecimal.ZERO);
                    busMaterial2.setInstallAmount2(BigDecimal.ZERO);
                    busMaterial2.setInstallAmount3(BigDecimal.ZERO);
                    busMaterial2.setMainsumPrice1(mainsumPrice1);
                    busMaterial2.setMainsumPrice2(mainsumPrice2);
                    busMaterial2.setMainsumPrice3(mainsumPrice3);
                    busMaterial2.setInstallsumPrice1(BigDecimal.ZERO);
                    busMaterial2.setInstallsumPrice2(BigDecimal.ZERO);
                    busMaterial2.setInstallsumPrice3(BigDecimal.ZERO);
                    busMaterial2.setContractsumSyntheticalPrice1(mainsumPrice1);
                    busMaterial2.setContractsumSyntheticalPrice2(mainsumPrice2);
                    busMaterial2.setContractsumSyntheticalPrice3(mainsumPrice3);
                    busMaterial2.setLoss(loss);
                    busMaterial2.setContractBillQuantity(contractBillQuantity);
                    busMaterial2.setPlanPurchaseCount(costQuantity100.multiply(loss));
                    busMaterial2.setConvertPurchaseCount(costQuantity100.multiply(loss));
                    busMaterial2.setParentId(0L);
                    busMaterial2.setCurrentMonthmainPrice(BigDecimal.ZERO);
                    busMaterial2.setCurrentMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial2.setCurrentMonthsumPrice(BigDecimal.ZERO);
                    busMaterial2.setLastMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial2.setLastMonthmainPrice(BigDecimal.ZERO);
                    busMaterial2.setLastMonthsumPrice(BigDecimal.ZERO);
                    busMaterial2.setTotalsumPrice(BigDecimal.ZERO);
                    busMaterial2.setTotalMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial2.setTotalMonthmainPrice(BigDecimal.ZERO);
                    Long id2 = IdWorker.getId();
//                    BeanUtils.copyProperties(busMaterial, busMaterial2);
                    busMaterial2.setMaterialId(id2);
                    busMaterial2.setParentId(0L);
                    //层级号 判断逻辑处理
                    if (FlymeUtils.isNotEmpty(levelNo)) {
                        if (levelNo.length() == 5) {
                            arrays.add(levelNo);
                        } else {
                            if (FlymeUtils.isNotEmpty(arrays)) {
                                for (String d : arrays) {
                                    if (d.equals(levelNo.substring(0, 5))) {
                                        Map<String, Object> busplanMap = Maps.newHashMap();
                                        busplanMap.put("levelNo", d);
                                        List<BusMaterialPlan100> plan100List = (List<BusMaterialPlan100>) busMaterialPlan100Service.listByMap(busplanMap);
                                        if (FlymeUtils.isNotEmpty(plan100List)) {
                                            busMaterial2.setParentId(plan100List.get(0).getMaterialId());
                                        }
                                    }
                                }
                            }
                        }
                    }

                    busMaterials.add(busMaterial2);
                    busPlan100.setMaterialPlanId(IdWorker.getId());
                    busPlan100.setOrderflg("0");//初始化开口订单状态
                    busPlan100.setStatus(0);//未转存100
                    busPlan100.setApplayStatus(0);//未审核状态
                    busPlan100.setMaterialId(id2);
                    busPlan100.setProjectId(projectId);
                    busPlan100.setVersionId(veersionId);
                    busPlan100.setMajorName(majorName);
                    busPlan100.setMajorCode(majorCode);
                    busPlan100.setPackageName(projectName);
                    busPlan100.setVersionStatus(1);
                    busPlan100.setPackageNo(projectCode);
                    busPlan100.setMaterialWbsCode(materialWbsCode);
                    busPlan100.setMaterialDesc(materialDesc);
                    busPlan100.setMaterialName(materialName);
                    busPlan100.setMaterialCode(materialCode);
                    busPlan100.setSubjectName(subjectName);
                    busPlan100.setContractDesc(contractDesc);
                    busPlan100.setMaterialArea(materialArea);
                    busPlan100.setMaterialUse(materialUse);
                    busPlan100.setCostdisplayQuantity100(costQuantity100);
                    busPlan100.setCostQuantity100(costQuantity100);
                    busPlan100.setCostQuantity200(costQuantity200);
                    busPlan100.setContractMainPrice1(contractMainPrice1);
                    busPlan100.setContractMainPrice2(contractInstallPrice2);
                    busPlan100.setContractMainPrice3(contractInstallPrice3);
                    busPlan100.setMeasurementUnit(measurementUnit);
                    busPlan100.setBrandName(brandName);
                    busPlan100.setRemark(remark);
                    busPlan100.setLoss(loss);
                    busPlan100.setTechnicalData(technicalData);
                    busPlan100.setMateriaSpecs(materiaSpecs);
                    busPlan100.setLevelNo(levelNo);
                    busPlan100.setZtPurchaseCount(BigDecimal.ZERO);
                    busPlan100.setDhPurchaseCount(BigDecimal.ZERO);
                    busPlan100.setPlanPurchaseCount(costQuantity100.multiply(loss));
                    busPlan100.setDisplayplanPurchaseCount(costQuantity100.multiply(loss));
                    busPlan100.setEndPurchaseCount(costQuantity100.multiply(loss));
                    busPlan100.setVersionStatus(1);
                    busPlan100.setApplayStatus(0);//未审批状态
                    materialPlan100s.add(busPlan100);
                    continue;
                }
                if(ObjectUtils.isNotEmpty(objectObjectMap.get("全物料代码"))){
                    BusMaterialPlan100 busPlan100=new BusMaterialPlan100();
                    Long id=IdWorker.getId();
                    BusMaterial busMaterial=new BusMaterial();
                    busMaterial.setParentId(0L);
                    //层级号 判断逻辑处理
                    if(FlymeUtils.isNotEmpty(levelNo)){
                        if(levelNo.length()==5){
                            arrays.add(levelNo);
                        }else{
                            if(FlymeUtils.isNotEmpty(arrays)){
                                for (String d: arrays ) {
                                    if(d.equals(levelNo.substring(0,5))){
                                        Map<String, Object> busplanMap = Maps.newHashMap();
                                        busplanMap.put("levelNo", d);
                                        List<BusMaterialPlan100> plan100List = (List<BusMaterialPlan100>) busMaterialPlan100Service.listByMap(busplanMap);
                                        if(FlymeUtils.isNotEmpty(plan100List)){
                                            busMaterial.setParentId(plan100List.get(0).getMaterialId());
                                        }
                                    }
                                }
                            }
                        }
                    }
                    busMaterial.setMaterialId(id);
                    busMaterial.setProjectId(projectId);
                    busMaterial.setConversionUnit(conversionUnit);
                    busMaterial.setMeasurementUnit(measurementUnit);
                    busMaterial.setBeforeMarkQuantity(beforeMarkQuantity);
                    busMaterial.setMaterialId(id);
                    busMaterial.setProjectId(projectId);
                    busMaterial.setConversionUnit(conversionUnit);
                    busMaterial.setMeasurementUnit(measurementUnit);
                    busMaterial.setMainPrice(mainPrice);
                    busMaterial.setMainPrice1(contractMainPrice1);
                    busMaterial.setMainPrice2(contractMainPrice2);
                    busMaterial.setMainPrice3(contractMainPrice3);
                    busMaterial.setInstallPrice(BigDecimal.ZERO);
                    busMaterial.setInstallPrice1(BigDecimal.ZERO);
                    busMaterial.setInstallPrice2(BigDecimal.ZERO);
                    busMaterial.setInstallPrice3(BigDecimal.ZERO);
                    busMaterial.setSyntheticalPrice1(contractMainPrice1);//有合同综合价
                    busMaterial.setSyntheticalPrice2(contractMainPrice2);//拟合同综合价
                    busMaterial.setSyntheticalPrice3(contractMainPrice3);//合同综合价
                    busMaterial.setSyntheticalPrice(mainPrice);//预算成本综合价
                    busMaterial.setSyntheticalAmount(mainPrice.multiply(costQuantity100));//预算成本合计
                    busMaterial.setSyntheticalAmount1(contractMainPrice1.multiply(costQuantity100));//有合同预算成本合计
                    busMaterial.setSyntheticalAmount2(contractMainPrice2.multiply(costQuantity100));//拟合同预算成本合计
                    busMaterial.setSyntheticalAmount3(contractMainPrice3.multiply(costQuantity100));//合同预算成本合计
                    busMaterial.setContractBillQuantity(contractBillQuantity);
                    busMaterial.setMainAmount(mainAmount);//成本主材费
                    busMaterial.setMainAmount1(mainAmount1);
                    busMaterial.setMainAmount2(mainAmount2);
                    busMaterial.setMainAmount3(mainAmount3);
                    busMaterial.setInstallAmount(BigDecimal.ZERO);//成本安装价
                    busMaterial.setInstallAmount1(BigDecimal.ZERO);
                    busMaterial.setInstallAmount2(BigDecimal.ZERO);
                    busMaterial.setInstallAmount3(BigDecimal.ZERO);
                    busMaterial.setMainsumPrice1(mainsumPrice1);
                    busMaterial.setMainsumPrice2(mainsumPrice2);
                    busMaterial.setMainsumPrice3(mainsumPrice3);
                    busMaterial.setInstallsumPrice1(BigDecimal.ZERO);
                    busMaterial.setInstallsumPrice2(BigDecimal.ZERO);
                    busMaterial.setInstallsumPrice3(BigDecimal.ZERO);
                    busMaterial.setContractsumSyntheticalPrice1(mainsumPrice1);
                    busMaterial.setContractsumSyntheticalPrice2(mainsumPrice2);
                    busMaterial.setContractsumSyntheticalPrice3(mainsumPrice3);
                    busMaterial.setLoss(loss);
                    busMaterial.setContractBillQuantity(contractBillQuantity);
                    busMaterial.setPlanPurchaseCount(costQuantity100.multiply(loss));
                    busMaterial.setConvertPurchaseCount(costQuantity100.multiply(loss));
                    busMaterial.setParentId(0L);
                    busMaterial.setCurrentMonthmainPrice(BigDecimal.ZERO);
                    busMaterial.setCurrentMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial.setCurrentMonthsumPrice(BigDecimal.ZERO);
                    busMaterial.setLastMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial.setLastMonthmainPrice(BigDecimal.ZERO);
                    busMaterial.setLastMonthsumPrice(BigDecimal.ZERO);
                    busMaterial.setTotalsumPrice(BigDecimal.ZERO);
                    busMaterial.setTotalMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial.setTotalMonthmainPrice(BigDecimal.ZERO);
                    busMaterials.add(busMaterial);
                    busPlan100.setMaterialPlanId(id);
                    busPlan100.setOrderflg("0");//初始化开口订单状态
                    busPlan100.setStatus(0);//未转存100
                    busPlan100.setApplayStatus(0);//未审核状态
                    busPlan100.setMaterialId(id);
                    busPlan100.setVersionStatus(1);
                    busPlan100.setProjectId(projectId);
                    busPlan100.setVersionId(veersionId);
                    busPlan100.setMajorName(majorName);
                    busPlan100.setMajorCode(majorCode);
                    busPlan100.setPackageName(projectName);
                    busPlan100.setPackageNo(projectCode);
                    busPlan100.setMaterialWbsCode(materialWbsCode);
                    busPlan100.setMaterialDesc(materialDesc);
                    busPlan100.setMaterialName(materialName);
                    busPlan100.setMaterialCode(materialCode);
                    busPlan100.setSubjectName(subjectName);
                    busPlan100.setContractDesc(contractDesc);
                    busPlan100.setMaterialArea(materialArea);
                    busPlan100.setMaterialUse(materialUse);
                    busPlan100.setCostdisplayQuantity100(costQuantity100);
                    busPlan100.setCostQuantity100(costQuantity100);
                    busPlan100.setCostQuantity200(costQuantity200);
                    busPlan100.setContractMainPrice1(contractMainPrice1);
                    busPlan100.setContractMainPrice2(contractMainPrice2);
                    busPlan100.setContractMainPrice3(contractMainPrice3);
                    busPlan100.setMeasurementUnit(measurementUnit);
                    busPlan100.setBrandName(brandName);
                    busPlan100.setRemark(remark);
                    busPlan100.setLevelNo(levelNo);
                    busPlan100.setLoss(loss);
                    busPlan100.setTechnicalData(technicalData);
                    busPlan100.setMateriaSpecs(materiaSpecs);
                    busPlan100.setZtPurchaseCount(BigDecimal.ZERO);
                    busPlan100.setDhPurchaseCount(BigDecimal.ZERO);
                    busPlan100.setPlanPurchaseCount(costQuantity100.multiply(loss));
                    busPlan100.setDisplayplanPurchaseCount(costQuantity100.multiply(loss));
                    busPlan100.setEndPurchaseCount(costQuantity100.multiply(loss));
                    materialPlan100s.add(busPlan100);
                }else{
                    BusMaterialPackage100 busPackage100 = new BusMaterialPackage100();
                    Long id = IdWorker.getId();
                    busPackage100.setMaterialPackage100Id(id);
                    BusMaterial busMaterial = new BusMaterial();
                    busMaterial.setParentId(0L);
                    if (FlymeUtils.isNotEmpty(levelNo)) {
                        if (levelNo.length() == 5) {
                            arrays.add(levelNo);
                        } else {
                            if (FlymeUtils.isNotEmpty(arrays)) {
                                for (String d : arrays) {
                                    if (d.equals(levelNo.substring(0, 5))) {
                                        Map<String, Object> busplanMap = Maps.newHashMap();
                                        busplanMap.put("levelNo", d);
                                        List<BusMaterialPlan100> plan100List = (List<BusMaterialPlan100>) busMaterialPlan100Service.listByMap(busplanMap);
                                        if (FlymeUtils.isNotEmpty(plan100List)) {
                                            busMaterial.setParentId(plan100List.get(0).getMaterialId());
                                        }
                                    }
                                }
                            }
                        }
                    }

                    busMaterial.setBeforeMarkQuantity(beforeMarkQuantity);
                    busMaterial.setMaterialId(id);
                    busMaterial.setProjectId(projectId);
                    busMaterial.setConversionUnit(conversionUnit);
                    busMaterial.setMeasurementUnit(measurementUnit);
                    busMaterial.setMainPrice(BigDecimal.ZERO);
                    busMaterial.setMainPrice1(BigDecimal.ZERO);
                    busMaterial.setMainPrice2(BigDecimal.ZERO);
                    busMaterial.setMainPrice3(BigDecimal.ZERO);
                    busMaterial.setInstallPrice(installPrice);
                    busMaterial.setInstallPrice1(contractInstallPrice1);
                    busMaterial.setInstallPrice2(contractInstallPrice2);
                    busMaterial.setInstallPrice3(contractInstallPrice3);
                    busMaterial.setSyntheticalPrice1(contractInstallPrice1);//有合同综合价
                    busMaterial.setSyntheticalPrice2(contractInstallPrice2);//拟合同综合价
                    busMaterial.setSyntheticalPrice3(contractInstallPrice3);//合同综合价
                    busMaterial.setSyntheticalPrice(installPrice);//预算成本综合价
                    busMaterial.setSyntheticalAmount(installPrice.multiply(costQuantity100));//预算成本合计
                    busMaterial.setSyntheticalAmount1(contractInstallPrice1.multiply(costQuantity100));//有合同预算成本合计
                    busMaterial.setSyntheticalAmount2(contractInstallPrice2.multiply(costQuantity100));//拟合同预算成本合计
                    busMaterial.setSyntheticalAmount3(contractInstallPrice3.multiply(costQuantity100));//合同预算成本合计
                    busMaterial.setContractBillQuantity(contractBillQuantity);
                    busMaterial.setMainAmount(BigDecimal.ZERO);//成本主材费
                    busMaterial.setMainAmount1(BigDecimal.ZERO);
                    busMaterial.setMainAmount2(BigDecimal.ZERO);
                    busMaterial.setMainAmount3(BigDecimal.ZERO);
                    busMaterial.setInstallAmount(installAmount);//成本安装价
                    busMaterial.setInstallAmount1(installAmount1);
                    busMaterial.setInstallAmount2(installAmount2);
                    busMaterial.setInstallAmount3(installAmount3);
                    busMaterial.setMainsumPrice1(BigDecimal.ZERO);
                    busMaterial.setMainsumPrice2(BigDecimal.ZERO);
                    busMaterial.setMainsumPrice3(BigDecimal.ZERO);
                    busMaterial.setInstallsumPrice1(installsumPrice1);
                    busMaterial.setInstallsumPrice2(installsumPrice2);
                    busMaterial.setInstallsumPrice3(installsumPrice3);
                    busMaterial.setContractsumSyntheticalPrice1(installsumPrice1);
                    busMaterial.setContractsumSyntheticalPrice2(installsumPrice2);
                    busMaterial.setContractsumSyntheticalPrice3(installsumPrice3);
                    busMaterial.setLoss(loss);
                    busMaterial.setContractBillQuantity(contractBillQuantity);
                    busMaterial.setPlanPurchaseCount(costQuantity100.multiply(loss));
                    busMaterial.setConvertPurchaseCount(costQuantity100.multiply(loss));
                    busMaterial.setParentId(0L);
                    busMaterial.setCurrentMonthmainPrice(BigDecimal.ZERO);
                    busMaterial.setCurrentMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial.setCurrentMonthsumPrice(BigDecimal.ZERO);
                    busMaterial.setLastMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial.setLastMonthmainPrice(BigDecimal.ZERO);
                    busMaterial.setLastMonthsumPrice(BigDecimal.ZERO);
                    busMaterial.setTotalsumPrice(BigDecimal.ZERO);
                    busMaterial.setTotalMonthinstallPrice(BigDecimal.ZERO);
                    busMaterial.setTotalMonthmainPrice(BigDecimal.ZERO);
                    busMaterials.add(busMaterial);
                    busPackage100.setProjectId(projectId);
                    busPackage100.setMaterialPackage100Id(id);
                    busPackage100.setTopParentId(id);
                    busPackage100.setParentId(0L);
                    busPackage100.setVersionId(packageVesionId);
                    busPackage100.setMaterialId(id);
                    busPackage100.setMajorName(majorName);
                    busPackage100.setMajorCode(majorCode);
                    busPackage100.setPackageName(projectName);
                    busPackage100.setPackageNo(projectCode);
                    busPackage100.setServiceCode(serviceCode);
                    busPackage100.setServiceWbsCode(serviceWbsCode);
                    busPackage100.setSubjectName(subjectName);
                    busPackage100.setPackageDesc(packageDesc);
                    busPackage100.setLevelNo(levelNo);
                    busPackage100.setContractDesc(contractDesc);
                    busPackage100.setMaterialArea(materialArea);
                    busPackage100.setMaterialUse(materialUse);
                    busPackage100.setCostQuantity100(costQuantity100);
                    busPackage100.setCostQuantity200(costQuantity200);
                    busPackage100.setPackageDesc(packageDesc);
                    busPackage100.setContractInstallPrice1(contractInstallPrice1);
                    busPackage100.setContractInstallPrice2(contractInstallPrice2);
                    busPackage100.setContractInstallPrice3(contractInstallPrice3);
                    busPackage100.setContractOtherPrice1(syntheticalPrice1);
                    busPackage100.setContractOtherPrice2(syntheticalPrice2);
                    busPackage100.setContractOtherPrice3(syntheticalPrice3);
                    busPackage100.setLevelNo(levelNo);
                    busPackage100.setStatus(0);//初始化导入状态 0 未转100 1转为100
                    busPackage100.setVersionStatus(1);
                    busPackage100.setApplayStatus(null);//未审批状态
                    busPackage100.setMateriaSpecs(materiaSpecs);//项目名称
                    busMaterialPackage100List.add(busPackage100);
                }
                i++;
            }
            //100分包初始化导入
            boolean a = busMaterialPackage100Service.saveBatch(busMaterialPackage100List);
            //100材料初始化数据导入
            boolean b = busMaterialPlan100Service.saveBatch(materialPlan100s);
            //材料计算列对应表
            boolean c = bizService.saveBatch(busMaterials);
            if (a & b & c) {
                project.setVersionId(veersionId);//项目的初始化版本
                project.setVersionpackageId(packageVesionId);
                project.setUserId(OpenHelper.getUserId());//导入数据操作人
                project.setVersion100(0);
                busProjectService.saveOrUpdate(project);
                busVersionService.save(version);//保存材料版本信息
                busVersionService.save(pversion);//保存分包版本信息
            }
        }
        return ResultBody.ok();
    }

    /**
     * 导入项目材料母表数据
     *
     */
    @PostMapping(value = "importExcelTest")
    public ResultBody importExcelTest(@RequestParam("file") MultipartFile file, Long projectId) throws Exception {
        if(null == projectId) {
            return ResultBody.failed("项目信息丢失。");
        }
        StaticLog.info("importExcelTest------start");
        StaticLog.info("excelList------start");
        List<Map<Object, Object>> excelList = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, new ImportParams());
        StaticLog.info("excelList------end");
        String redisKey = String.valueOf(projectId) + "_bfCk";
        if(null != redisUtils.getList(redisKey)) {
            redisUtils.del(redisKey);
        }
        redisUtils.setList(redisKey, excelList);
        StaticLog.info("importExcelTest------end");
        return ResultBody.ok(excelList);
    }

    /**
     * 导入项目材料母表数据
     *
     */
    @PostMapping(value = "impTestCkResult")
    public ResultBody impTestCkResult(@RequestParam(required = false) Long projectId) throws ExecutionException, InterruptedException {
        String redisKey = String.valueOf(projectId) + "_bfCk";
        List<Map<Object, Object>> excelList = redisUtils.getList(redisKey);
        if(null == excelList || 0 == excelList.size()) {
            return ResultBody.failed("文件导入失败，请重新导入");
        }
        redisUtils.del(redisKey);
        return pmsApiUtil.impTestCkResult(excelList, projectId);
    }
}

